package com.shelpe.services.scheduler.item.creative;


import java.util.Calendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;

import com.shelpe.services.scheduler.dto.RptCreativeDto;
import com.shelpe.services.scheduler.mongorepository.UnrecoverableErrorRepository;
import com.shelpe.services.scheduler.po.Adgroups;
import com.shelpe.services.scheduler.po.SyncLog;
import com.shelpe.services.scheduler.syncclient.CreativeClient;
import com.shelpe.services.scheduler.util.Util;


public class RptCreativeProcessor implements ItemProcessor<Adgroups, SyncLog>{
	
	private static final Logger logger = LoggerFactory.getLogger(RptCreativeProcessor.class);
	
	@Autowired
	private CreativeClient creClient;
	
	@Autowired
	private UnrecoverableErrorRepository errorRep;
	
	@Override
	public SyncLog process(Adgroups item) throws Exception {
		// TODO Auto-generated method stub
		if(!item.getOnlineStatus().equalsIgnoreCase("online")){
			return null;
		}
		
		long userId = item.getUserId();
		if(errorRep.countByUserId(userId) > 0){
			return null;
		}
		
		long adgroupId = item.getAdgroupId();
		long campaignId = item.getCampaignId();
		Calendar cal = Calendar.getInstance();	  
		int hour=cal.get(Calendar.HOUR_OF_DAY);//小时
		String record = hour > 8 ? "no" : "yes";
		RptCreativeDto rptDto = creClient.syncRptCreativesByUserId(userId, campaignId, adgroupId, record);
		
		return Util.convert(rptDto);
	}
	
}
